package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.pinyougou.mapper.BrandMapper;
import com.pinyougou.pojo.TbBrand;

import com.pinyougou.sellergoods.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;


    @Override
    public PageInfo<TbBrand> getAll(TbBrand tbBrand,int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(TbBrand.class);
        Example.Criteria criteria = example.createCriteria();
        /**
         * 动态sql查询 拼接查询语句
         */
        if(tbBrand!=null){
            if (StringUtils.isNotBlank(tbBrand.getName())){
                criteria.andLike("name","%"+tbBrand.getName()+"%");
            }
            if (StringUtils.isNotBlank(tbBrand.getFirstChar())){
                criteria.andEqualTo("firstChar",tbBrand.getFirstChar());
            }
        }
        List<TbBrand> tbBrands = brandMapper.selectByExample(example);
        PageInfo<TbBrand> pageInfo = new PageInfo<>(tbBrands);
        return pageInfo;
    }

    @Override
    public List<TbBrand> findAll() {
        return brandMapper.selectAll();
    }

    @Override
    public int addBrand(TbBrand tbBrand) {
            return brandMapper.insert(tbBrand);
    }

    @Override
    public TbBrand findById(long id) {
        return brandMapper.selectByPrimaryKey(id);
    }
    @Override
    public int updateBrand(TbBrand tbBrand) {
        return brandMapper.updateByPrimaryKeySelective(tbBrand);
    }

    @Override
    public int delete(List<Long> ids) {
        Example example = new Example(TbBrand.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id",ids);
        return brandMapper.deleteByExample(example);
    }
}
